package com.example.demo.Service.impl;

import com.example.demo.Dao.SysRoleMapper;
import com.example.demo.Dao.SysUserMapper;
import com.example.demo.Entity.SysRole;
import com.example.demo.Entity.SysUser;
import com.example.demo.Service.SysUserService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.defaults.DefaultSqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public SysUser findByUsername(String username) {
        return sysUserMapper.findUserByName(username);
    }

    @Override
    public String findSaltByUsername(String username) {
        return sysUserMapper.findSaltByUsername(username);
    }

    @Override
    public boolean hasUser(String username) {
        if(findByUsername(username)==null){
            return false;
        }else {
            return true;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addUser(String username, String password, String salt) {
        sysUserMapper.addUser(username,password,salt);
        SysUser sysUser=sysUserMapper.findUserByName(username);
        sysRoleMapper.addUser(sysUser.getId());

    }

    @Override
    public List<SysRole> getSysRoleList(Integer id) {
        return sysRoleMapper.getRolesByUserId(id);
    }
}
